package hlhj.fhp.supreme.activitys

import android.content.Intent
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.Toast
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.hyphenate.chat.EMChatRoom
import com.hyphenate.chat.EMClient
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import kotlinx.android.synthetic.main.activity_jie_pan_aty.*
import java.util.ArrayList
import com.hyphenate.chat.EMGroupInfo
import com.hyphenate.chat.EMCursorResult
import fhp.hlhj.giantfold.utils.LogUtil
import com.hyphenate.chat.EMGroup
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import fhp.hlhj.giantfold.utils.ToastUtils
import hlhj.fhp.supreme.adapters.JiePanListAdp
import hlhj.fhp.supreme.adapters.JiePanPagerAdp
import hlhj.fhp.supreme.customview.JiePanPayDialog
import hlhj.fhp.supreme.fragment.JiePanListFgm
import hlhj.fhp.supreme.javabean.BaseBean
import hlhj.fhp.supreme.javabean.JiePanCatBean
import hlhj.fhp.supreme.javabean.PhoneBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import kotlinx.android.synthetic.main.read_head.view.*


class JiePanAty : BaseAty() {
    private val datas=ArrayList<EMGroupInfo>()
//    private lateinit var adp:BaseQuickAdapter<EMGroupInfo,BaseViewHolder>
//    private lateinit var head: View
    private val tabs=ArrayList<JiePanCatBean.DataBean>()
    private val tempDatas=ArrayList<String>()
    private lateinit var pagerAdp:JiePanPagerAdp
    private val fgms=ArrayList<Fragment>()
    override fun getContentId(): Int = R.layout.activity_jie_pan_aty
    override fun initView() {
       /* head= LayoutInflater.from(this).inflate(R.layout.read_head,null)
        Thread(Runnable {
            runOnUiThread {
                loadingDialog.show()
            }
//            val result = EMClient.getInstance().chatroomManager().fetchPublicChatRoomsFromServer(100, "")
            val result2 = EMClient.getInstance().groupManager().getPublicGroupsFromServer(100, "")//需异步处理
            LogUtil.log("聊天室---${result2.data.toString()}")
            datas.addAll(result2.data)
            runOnUiThread {
                loadingDialog.dismiss()
            }
            adp=object :BaseQuickAdapter<EMGroupInfo,BaseViewHolder>(R.layout.jiepan_list_item,datas){
                override fun convert(helper: BaseViewHolder?, item: EMGroupInfo?) {
                    helper?.setText(R.id.itemTittle,item?.groupName)
                    Thread(Runnable {
                        val group = EMClient.getInstance().groupManager().getGroupFromServer(item?.groupId)
                        LogUtil.log("${group==null}")
                        if (group==null){
                            runOnUiThread {
                                helper?.setText(R.id.itemTime,"")
                            }
                        }else{
                            runOnUiThread {
                                LogUtil.log("聊天室名字---${group.description}")
                                helper?.setText(R.id.itemTime,group!!.description)
                            }
                        }
                    }).start()
                }
            }
            runOnUiThread {
                recyclerview.adapter=adp
                recyclerview.layoutManager=LinearLayoutManager(this,LinearLayout.VERTICAL,false)
                adp.setOnItemClickListener { adapter, view, position ->
                    OkGo.post<BaseBean>(Urls.BaseUrl+ Urls.IS_JIEPAN)
                            .execute(object : JsonCallBack<BaseBean>(this@JiePanAty){
                                override fun onSuccess(response: Response<BaseBean>?) {
                                    val bean = response?.body()
                                    if (bean?.code==1){
                                        Thread(Runnable {
                                            try {
                                                EMClient.getInstance().groupManager().joinGroup(datas[position].groupId)
                                            }catch (e:Exception){

                                            }
                                        }).start()
                                        val intent = Intent(this@JiePanAty, JiePanDetailAty::class.java)
                                        intent.putExtra("roomId",datas[position].groupId)
                                        intent.putExtra("name",datas[position].groupName)
                                        startActivity(intent)
                                    }else{
                                        JiePanPayDialog(this@JiePanAty,"20").show()

                                    }
                                }
                            })
                    //            startActivity(Intent(this,JiePanTextDetailAty::class.java))

                }
                adp.addHeaderView(head)
                head.btVip.visibility=View.GONE
            }

        }).start()
*/
        /*判断是否有权限*/
        judgeRight()
        pagerAdp= JiePanPagerAdp(fgms,supportFragmentManager)
        viewPager.adapter=pagerAdp
        tabLayout.setupWithViewPager(viewPager)
        /*使能tablayout*/
        initTab()
        enableTab()

        OkGo.post<PhoneBean>(Urls.BaseUrl+Urls.PHONE)
                .params("type","breakup_des")
                .execute(object :JsonCallBack<PhoneBean>(this@JiePanAty){
                    override fun onSuccess(response: Response<PhoneBean>?) {
                        val bean = response?.body()
                        if (bean!!.code==1){
                            tv2.text=bean?.data.breakup_des

                        }else{
                            ToastUtils.toast(this@JiePanAty,bean!!.msg)
                        }
                    }
                })

    }

    private fun judgeRight() {
        OkGo.post<BaseBean>(Urls.BaseUrl+Urls.IS_JIEPAN)
                .execute(object :JsonCallBack<BaseBean>(this@JiePanAty){
                    override fun onSuccess(response: Response<BaseBean>?) {
                        val body = response?.body()
                        body?.code?.let {
                            if (body.code==1){
                                btVip.visibility=View.GONE
                            }else{
                                btVip.visibility=View.VISIBLE
                            }
                        }
                    }
                })
    }

    private fun initTab() {
        tabLayout.tabMode= TabLayout.MODE_SCROLLABLE
        OkGo.post<JiePanCatBean>(Urls.BaseUrl+Urls.JIEPAN_CAT)
                .execute(object :JsonCallBack<JiePanCatBean>(this@JiePanAty){
                    override fun onSuccess(response: Response<JiePanCatBean>?) {
                        val body = response?.body()
                        if (body!!.code==1){
                            if (body.data!=null){
                                body.data.forEachIndexed { index, dataBean ->
                                    tabs.add(dataBean)
                                }
                            }
                            enableTab()
                        }else{
                            ToastUtils.toast(this@JiePanAty,"服务器错误")
                        }
                    }

                })
    }

    private fun enableTab() {
        for (i in 0 until tabs.size){
            val newTab = tabLayout.newTab()
            newTab.setText(tabs[i].name)
            tabLayout.addTab(newTab)
            val jiePanListFgm = JiePanListFgm.getInstence(tabs[i].id,tabs[i].name)
            fgms.add(jiePanListFgm)
        }
        pagerAdp.notifyDataSetChanged()

    }

    override fun initListener() {
        btExit.setOnClickListener {
            finish()
        }
        btVip.setOnClickListener {
            JiePanPayDialog(this@JiePanAty,"20").show()
        }

    }
}
